﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Common;
using System.Data.SqlClient;
using System.Reflection;
using System.Configuration;
using Instrument;

namespace UI
{
    public partial class BussinessForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        DataBaseOperation _dataBaseOperation;

        public BussinessForm()
        {
            InitializeComponent();
            _dataBaseOperation = DataBaseOperation.GetInstance();
            SelectAllPeople();
            SelectAllProduction();

        }
        void navBarControl_ActiveGroupChanged(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e)
        {
            navigationFrame.SelectedPageIndex = navBarControl.Groups.IndexOf(e.Group);
        }
        void barButtonNavigation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int barItemIndex = barSubItemNavigation.ItemLinks.IndexOf(e.Link);
            navBarControl.ActiveGroup = navBarControl.Groups[barItemIndex];
        }
        

        private void navBarItem3_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            navigationFrame.SelectedPageIndex = 3;
        }
        private void SelectAllPeople()
        {
            string sqlString = "SELECT * FROM UserInfo;";
            DataTable dt = new DataTable();
            using (SqlConnection conn = (SqlConnection)new SqlDatabaseProxy().CreateConnection())
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlString, (SqlConnection)new SqlDatabaseProxy().CreateConnection());
                sda.Fill(dt);
            }
            employeeGridControl.DataSource = dt;
        }
        private void SelectAllProduction()
        {
            string sqlString = "SELECT * FROM Production;";
            DataTable dt = new DataTable();
            using (SqlConnection conn = (SqlConnection)new SqlDatabaseProxy().CreateConnection())
            {
                SqlDataAdapter sda = new SqlDataAdapter(sqlString, (SqlConnection)new SqlDatabaseProxy().CreateConnection());
                sda.Fill(dt);
            }
            productionGridControl.DataSource = dt;
        }

        private void cardView1_Click(object sender, EventArgs e)
        {
            int d = 0;
        }

        private void cardView1_CardExpanded(object sender, DevExpress.XtraGrid.Views.Base.RowEventArgs e)
        {
            int d = 0;
        }

        private void layoutView1_CardClick(object sender, DevExpress.XtraGrid.Views.Layout.Events.CardClickEventArgs e)
        {
            //cardView1.a
        }

        private void productionGridControl_EditorKeyUp(object sender, KeyEventArgs e)
        {
            int i = 0;

        }

        private void cardView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow data = cardView1.GetDataRow(e.FocusedRowHandle);
            txtEditModel.EditValue = data[0];
            txtEditResCount.EditValue = data[1];
            txtEditQulifiedValue.EditValue = data[2];
            txtEditOffset.EditValue = data[3];
            txtEditRange.EditValue = data[4];
        }

        private void windowsUIButtonPanel1_Click(object sender, EventArgs e)
        {
            NewProduction form = new NewProduction();
            form.FormClosed += (s,m) => SelectAllProduction();
            form.ShowDialog();
        }

        private void AddUIButton_Click(object sender, EventArgs e)
        {
            PersonInfo form = new PersonInfo("New");
            form.FormClosing += (s, m) => SelectAllPeople();
            form.ShowDialog();
        }

        private void ModifyUIButton_Click(object sender, EventArgs e)
        {
            DataRow dr = employeeGridView.GetFocusedDataRow();
            PersonInfo form = new PersonInfo("Modify",dr);
            form.FormClosing += (s, m) => SelectAllPeople();
            form.ShowDialog();

        }

        private void skinRibbonGalleryBarItem_GalleryItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {
            string caption = string.Empty;

            if (skinRibbonGalleryBarItem.Gallery == null) return;

            caption = skinRibbonGalleryBarItem.Gallery.GetCheckedItems()[0].Tag.ToString();//获取真实的名称，没有汉化的

            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings["SkinString"].Value = caption;
            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("AppSettings");
        }

        private void BussinessForm_Load(object sender, EventArgs e)
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string Topic = configuration.AppSettings.Settings["SkinString"].Value;
            defaultLookAndFeel1.LookAndFeel.SetSkinStyle(Topic);//设置主题样式
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Production production = new Production();
            production.Model = txtEditModel.Text;
            production.ResistanceCount = Convert.ToInt32(txtEditResCount.EditValue);
            production.QualifiedResistance = Convert.ToDouble( txtEditQulifiedValue.EditValue);
            production.AllowOffset = Convert.ToDouble(txtEditOffset.EditValue);
            production.Range = Convert.ToDouble(txtEditRange.EditValue);
            DataRow dataRow = cardView1.GetFocusedDataRow();
            string oldModel = (string)dataRow[0];
            if(_dataBaseOperation.UpdateProduction(production,oldModel) == true)
            {
                _dataBaseOperation.AlterTableName(oldModel, production.Model);
                _dataBaseOperation.AlterColumns(Convert.ToInt32(dataRow[1]), production.ResistanceCount, production.Model);
                MessageBox.Show("操作成功");
            }
            else
            {
                MessageBox.Show("操作失败！");
            }
        }
    }
}